package com.bj58.mis.apm.server.kylinreceive.repository;

import com.bj58.mis.apm.server.kylinreceive.model.KylinProject;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;

/**
 * kylin项目对象操作jpa
 * @author wangxw
 */
public interface SearchRepository extends JpaRepository<KylinProject, Long> {

    @Query(value = "select id from kylin_project where name_en = :name and is_delete = 0 limit 1", nativeQuery = true)
    String queryProjectIdByName(@Param("name") String name);

    @Query(value = "select id from kylin_cube where name_en = :name and project_id = :projectId and is_delete = 0 limit 1", nativeQuery = true)
    String queryCubeIdByNameAndProjectId(@Param("name") String name, @Param("projectId") String projectId);

    @Query(value = "select id from kylin_segment where name = :name and cube_id = :cubeId and is_delete = 0 limit 1", nativeQuery = true)
    String querySegmentIdByName(@Param("name") String name, @Param("cubeId") String cubeId);

    @Query(value = "select max(m) from (\n" +
            "select max(last_modified) m from kylin_cube union all \n" +
            "select max(last_modified) m from kylin_cubedesc union all \n" +
            "select max(last_modified) m from kylin_model union all \n" +
            "select max(last_modified) m from kylin_project union all \n" +
            "select max(last_modified) m from kylin_table\n" +
            ") a", nativeQuery = true)
    Date queryLastModified();
}
